package geo.com.geosdkdemo.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import geo.com.geosdk.GeoManage;
import geo.com.geosdk.listener.DeviceCommandSendListener;
import geo.com.geosdk.modelsetting.BaseModel.BaseWorkingModeParameter;
import geo.com.geosdk.modelsetting.GeneralBean.DataLinkParameter;
import geo.com.geosdk.modelsetting.GeneralBean.SateSystemEnableParameter;
import geo.com.geosdkdemo.event.DialogDismissEvent;
import geo.com.geosdkdemo.R;

import static android.view.View.VISIBLE;


public class BaseSetupActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String ISBASE = "ISBASE";//移动站还是基准站
    //从主机得到的信息
    private BaseWorkingModeParameter baseWorkingModeParameter=GeoManage.getInstance().getBaseWorkingModeParameter();
    private DataLinkParameter dataLinkParameter = GeoManage.getInstance().getDataLinkParameter();
    private SateSystemEnableParameter systemEnableParameter=GeoManage.getInstance().getSystemEnableParameter();

    public static final String DIALOGNAME = "DIALOGNAME";
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private ImageView ivSwitchPpkOn;
    private ImageView ivSwitchPpkOff;
    private ConstraintLayout clPpk;
    private EditText etRecordPointname;
    private EditText etRecordInterval;
    private RelativeLayout rlDataLink;
    private RelativeLayout rlChannelNumber;
    private TextView tvDataLink;
    private ConstraintLayout clDataLinkNetwork;
    private RelativeLayout rlConnectMode;
    private TextView tvConnectModel;
    private ImageView ivSwitchConnectNetworkOn;
    private ImageView ivSwitchConnectNetworkOff;
    private EditText etCorsIp;
    private EditText etCorsPort;
    private EditText etCorsUser;
    private EditText etCorsPass;
    private EditText etCorsMountPoint;
    private Button btnDetermine;
    private FrameLayout flLoading;
    private ImageView ivLoading;
    private TextView tvLoadingTitle;
    private Animation anim;
    private ImageView ivSwitchGpsOn;
    private ImageView ivSwitchGpsOff;
    private ImageView ivSwitchGlonassOn;
    private ImageView ivSwitchGlonassOff;
    private ImageView ivSwitchBeidouOn;
    private ImageView ivSwitchBeidouOff;
    private ImageView ivSwitchGalileoOn;
    private ImageView ivSwitchGalileoOff;
    private ImageView ivSwitchSbasOn;
    private ImageView ivSwitchSbasOff;
    private ImageView ivSwitchQzssOn;
    private ImageView ivSwitchQzssOff;

    private RelativeLayout rlBaseStationId;
    private EditText etBaseStationId;
    private RelativeLayout rlStartModel;
    private TextView tvStartModel;
    private ConstraintLayout clStartupTypeRepeat;
    private EditText etLongitude;
    private EditText etLatitude;
    private EditText etAltitude;
    private RelativeLayout rlDifType;
    private TextView tvDifType;
    private ImageView ivSwitchAutoStartupOn;
    private ImageView ivSwitchAutoStartupOff;



    private ConstraintLayout clDataLinkUhf;
    private ConstraintLayout clCors;
    private TextView tvChannelNumber;
    private EditText etFrequency;
    private RelativeLayout rlProtocol;
    private TextView tvProtocol;
    private ConstraintLayout clDataLinkExternal;
    private EditText etBaud;
    private String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_setup);
        initView();
        initData();
    }

    private void initView() {
        ivCommonBack=findViewById(R.id.iv_common_back);
        tvCommonTitle=findViewById(R.id.tv_common_title);
        ivSwitchPpkOn = (ImageView)findViewById( R.id.iv_switch_ppk_on );
        ivSwitchPpkOff = (ImageView)findViewById( R.id.iv_switch_ppk_off );
        clPpk = (ConstraintLayout)findViewById( R.id.cl_ppk );
        etRecordPointname = (EditText)findViewById( R.id.et_record_pointname );
        etRecordInterval = (EditText)findViewById( R.id.et_record_interval );
        rlDataLink = (RelativeLayout)findViewById( R.id.rl_data_link );
        tvDataLink = (TextView)findViewById( R.id.tv_data_link);
        clDataLinkNetwork = (ConstraintLayout)findViewById( R.id.cl_data_link_network);
        rlConnectMode = (RelativeLayout)findViewById( R.id.rl_connect_mode );
        rlChannelNumber = (RelativeLayout)findViewById( R.id.rl_channel_number );
        tvConnectModel = (TextView)findViewById( R.id.tv_connect_model );
        ivSwitchConnectNetworkOn = (ImageView)findViewById( R.id.iv_switch_connect_network_on );
        ivSwitchConnectNetworkOff = (ImageView)findViewById( R.id.iv_switch_connect_network_off );
        etCorsIp = (EditText)findViewById( R.id.et_cors_ip );
        etCorsPort = (EditText)findViewById( R.id.et_cors_port );
        etCorsUser = (EditText)findViewById( R.id.et_cors_user );
        etCorsPass = (EditText)findViewById( R.id.et_cors_pass );
        etCorsMountPoint = (EditText)findViewById( R.id.et_cors_mount_point );
        btnDetermine = (Button)findViewById( R.id.btn_determine );
        flLoading = (FrameLayout)findViewById( R.id.fl_loading );
        ivLoading = (ImageView)findViewById( R.id.iv_loading );
        tvLoadingTitle = (TextView)findViewById( R.id.tv_loading_title );
        ivSwitchGpsOn = (ImageView)findViewById( R.id.iv_switch_gps_on );
        ivSwitchGpsOff = (ImageView)findViewById( R.id.iv_switch_gps_off );
        ivSwitchGlonassOn = (ImageView)findViewById( R.id.iv_switch_glonass_on );
        ivSwitchGlonassOff = (ImageView)findViewById( R.id.iv_switch_glonass_off );
        ivSwitchBeidouOn = (ImageView)findViewById( R.id.iv_switch_beidou_on );
        ivSwitchBeidouOff = (ImageView)findViewById( R.id.iv_switch_beidou_off );
        ivSwitchGalileoOn = (ImageView)findViewById( R.id.iv_switch_galileo_on );
        ivSwitchGalileoOff = (ImageView)findViewById( R.id.iv_switch_galileo_off );
        ivSwitchSbasOn = (ImageView)findViewById( R.id.iv_switch_sbas_on );
        ivSwitchSbasOff = (ImageView)findViewById( R.id.iv_switch_sbas_off );
        ivSwitchQzssOn = (ImageView)findViewById( R.id.iv_switch_qzss_on );
        ivSwitchQzssOff = (ImageView)findViewById( R.id.iv_switch_qzss_off );

        clDataLinkUhf = (ConstraintLayout)findViewById( R.id.cl_data_link_uhf );
        clCors = (ConstraintLayout)findViewById( R.id.cl_cors );
        tvChannelNumber = (TextView)findViewById( R.id.tv_channel_number );
        etFrequency = (EditText)findViewById( R.id.et_frequency );
        rlProtocol = (RelativeLayout)findViewById( R.id.rl_protocol );
        tvProtocol = (TextView)findViewById( R.id.tv_protocol );
        clDataLinkExternal = (ConstraintLayout)findViewById( R.id.cl_data_link_external );
        etBaud = (EditText)findViewById( R.id.et_baud );

        rlBaseStationId = (RelativeLayout)findViewById( R.id.rl_base_station_id );
        etBaseStationId = (EditText)findViewById( R.id.et_base_station_id );
        rlStartModel = (RelativeLayout)findViewById( R.id.rl_start_model );
        tvStartModel = (TextView)findViewById( R.id.tv_start_model );
        clStartupTypeRepeat = (ConstraintLayout)findViewById( R.id.cl_startup_type_repeat );
        etLongitude = (EditText)findViewById( R.id.et_longitude );
        etLatitude = (EditText)findViewById( R.id.et_latitude );
        etAltitude = (EditText)findViewById( R.id.et_altitude );
        rlDifType = (RelativeLayout)findViewById( R.id.rl_difType );
        tvDifType = (TextView)findViewById( R.id.tv_difType );
        ivSwitchAutoStartupOn = (ImageView)findViewById( R.id.iv_switch_auto_startup_on );
        ivSwitchAutoStartupOff = (ImageView)findViewById( R.id.iv_switch_auto_startup_off );


        ivCommonBack.setOnClickListener(this);
        ivSwitchPpkOn.setOnClickListener(this);
        ivSwitchPpkOff.setOnClickListener(this);
        rlDataLink.setOnClickListener(this);
        rlConnectMode.setOnClickListener(this);
        ivSwitchConnectNetworkOn.setOnClickListener(this);
        ivSwitchConnectNetworkOff.setOnClickListener(this);
        ivSwitchGpsOn.setOnClickListener(this);
        ivSwitchGpsOff.setOnClickListener(this);
        ivSwitchGlonassOn.setOnClickListener(this);
        ivSwitchGlonassOff.setOnClickListener(this);
        ivSwitchBeidouOn.setOnClickListener(this);
        ivSwitchBeidouOff.setOnClickListener(this);
        ivSwitchGalileoOff.setOnClickListener(this);
        ivSwitchGalileoOn.setOnClickListener(this);
        ivSwitchSbasOff.setOnClickListener(this);
        ivSwitchSbasOn.setOnClickListener(this);
        ivSwitchQzssOn.setOnClickListener(this);
        ivSwitchQzssOff.setOnClickListener(this);
        rlProtocol.setOnClickListener(this);
        rlChannelNumber.setOnClickListener(this);
        btnDetermine.setOnClickListener(this);
        ivSwitchAutoStartupOn.setOnClickListener(this);
        ivSwitchAutoStartupOff.setOnClickListener(this);


        rlStartModel.setOnClickListener(this);
        rlDifType.setOnClickListener(this);

    }

    private void initData() {
        tvCommonTitle.setText("基准站设置");
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        //进来显示接收机解析的数据
        showRTKData();
    }

    private void showRTKData() {
        //显示通用
        etBaseStationId.setText(baseWorkingModeParameter.mBaseSiteID);
        String startModel=baseWorkingModeParameter.mBaseStartupType.getTypeTitle(baseWorkingModeParameter.mBaseStartupType);
        tvStartModel.setText(startModel);
        if(startModel.equals("指定基站坐标")){
            clStartupTypeRepeat.setVisibility(VISIBLE);
            etLongitude.setText(String.valueOf(baseWorkingModeParameter.BLHCor.getDLongitude()));
            etLatitude.setText(String.valueOf(baseWorkingModeParameter.BLHCor.getDLatitude()));
            etAltitude.setText(String.valueOf(baseWorkingModeParameter.BLHCor.getDAltitude()));
        }
        tvDifType.setText(baseWorkingModeParameter.mDifType.getTypeTitle(baseWorkingModeParameter.mDifType));
        if(baseWorkingModeParameter.mBaseAutoStartup){
            onClickAutoStartupOff();
        }else{
            onClickAutoStartupOn();
        }
        boolean mUsePPK = baseWorkingModeParameter.mUsePPK;
        if(mUsePPK){
            onClickPPkOff();
            etRecordPointname.setText(baseWorkingModeParameter.ppkParameter.mRecordPointname);
            etRecordInterval.setText(String.valueOf(baseWorkingModeParameter.ppkParameter.mRecordInterval));
        }else{
            onClickPPkOn();
        }
        //显示数据链
        String typeTitle = dataLinkParameter.mDataLinkType.getTypeTitle(dataLinkParameter.mDataLinkType);
        showDataLinkData(typeTitle);

        //显示卫星系统
        showSatelliteSystem();

    }

    private void showDataLinkData(String datalink) {
        tvDataLink.setText(datalink);
        switch (datalink){
            case "主机网络":
                showDatalinkNetwork();
                break;
            case "内置电台":
                showDatalinkUHF();
                break;
            case "外置电台":
                showDatalinkExt();
                break;
            default:
                showDatalinkOther();
                break;
        }
    }

    private void showDatalinkOther() {
        clDataLinkExternal.setVisibility(View.GONE);
        clDataLinkUhf.setVisibility(View.GONE);
        clDataLinkNetwork.setVisibility(View.GONE);
    }


    private void showDatalinkExt() {
        clDataLinkExternal.setVisibility(View.VISIBLE);
        clDataLinkUhf.setVisibility(View.GONE);
        clDataLinkNetwork.setVisibility(View.GONE);

        etBaud.setText(String.valueOf(dataLinkParameter.mExternal.Baud));
    }

    private void showDatalinkUHF() {
        clDataLinkUhf.setVisibility(View.VISIBLE);
        clDataLinkNetwork.setVisibility(View.GONE);
        clDataLinkExternal.setVisibility(View.GONE);

        int channelNum=dataLinkParameter.mUHF.mChannelNumber;
        tvChannelNumber.setText(String.valueOf(channelNum));
        if(channelNum==8){
            etFrequency.setEnabled(true);
        }else{
            etFrequency.setEnabled(false);
        }
        //每一个通道对应一个频率，通道1对应mFrequencyofChannel[0]
        etFrequency.setText(String.valueOf(dataLinkParameter.mUHF.mFrequencyofChannel[channelNum-1]));
        tvProtocol.setText(dataLinkParameter.mUHF.mUhfProtocolType.getTypeTitle(dataLinkParameter.mUHF.mUhfProtocolType));
    }

    //显示网络数据链
    private void showDatalinkNetwork() {
        clDataLinkNetwork.setVisibility(View.VISIBLE);
        clDataLinkUhf.setVisibility(View.GONE);
        clDataLinkExternal.setVisibility(View.GONE);
        String connecModel=dataLinkParameter.mNetwork.mConnectionMode.getTypeTitle(dataLinkParameter.mNetwork.mConnectionMode);
        tvConnectModel.setText(connecModel);
        if(dataLinkParameter.mNetwork.mAutoConnect){
            onClickAutoConnectNetOff();
        }else{
            onClickAutoConnectNetOn();
        }

        etCorsIp.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrIP);
        etCorsPort.setText(String.valueOf(dataLinkParameter.mNetwork.mCorsParameter.mPort));

        if(connecModel.equals("TCP客户端")){//此时不需要显示CORS用户名密码，接入点
            clCors.setVisibility(View.GONE);
            return;
        }

        etCorsUser.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrUser);
        etCorsPass.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrPassword);
        etCorsMountPoint.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrMountPoint);
    }

    private void showSatelliteSystem() {
        if(systemEnableParameter.UseGPS){
            onClickGpsOff();
        }else{
            onClickGpsOn();
        }

        if(systemEnableParameter.UseGlonass){
            onClickGlonassOff();
        }else{
            onClickGlonassOn();
        }

        if(systemEnableParameter.UseBeiDou){
            onClickBeidouOff();
        }else{
            onClickBeidouOn();
        }

        if(systemEnableParameter.UseGalileo){
            onClickGalileoOff();
        }else{
            onClickGalileoOn();
        }

        if(systemEnableParameter.UseSBAS){
            onClickSbasOff();
        }else{
            onClickSbasOn();
        }

        if(systemEnableParameter.UseQZSS){
            onClickQzssOff();
        }else{
            onClickQzssOn();
        }

        if(systemEnableParameter.UseGPS){
            onClickGpsOff();
        }else{
            onClickGpsOn();
        }


    }


    private void onClickBtnDetermine() {
        //开始设置命令
        //1.开始动画
        startAnim();
        //2.监听发送状态
        GeoManage.getInstance().setDeviceCommandSendListener(new DeviceCommandSendListener() {
            @Override
            public void OnDeviceCommandSendCallBack(final int status, final String strFunction) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(status==0){
                            str="开始"+strFunction;
                        }else if(status==1){
                            str=strFunction+"完成";
                        }else if(status==2){
                            str=strFunction+"超时";
                            //超时这边的操作是继续发送下一条命令
                        }else if(status==3){//全部发送成功
                            str="全部命令发送完成";
                            stopAnim();
                            finish();
                        }
                        tvLoadingTitle.setText(str);
                        Log.d("OnDeviceCommandSend", "OnDeviceCommandSendCallBack: "+str);
                    }
                });

            }
        });

        //设置参数
        //1.设置基准站通用选项
        BaseWorkingModeParameter baseWorkingModeParameter=new BaseWorkingModeParameter();
        baseWorkingModeParameter.mBaseSiteID=etBaseStationId.getText().toString().trim();
        String startType=tvStartModel.getText().toString().trim();
        baseWorkingModeParameter.mBaseStartupType=baseWorkingModeParameter.mBaseStartupType.titleToEnum(startType);
        if(startType.equals("指定基站坐标")){
            baseWorkingModeParameter.BLHCor.setDLongitude(Double.parseDouble(etLongitude.getText().toString().trim()));
            baseWorkingModeParameter.BLHCor.setDLatitude(Double.parseDouble(etLatitude.getText().toString().trim()));
            baseWorkingModeParameter.BLHCor.setDAltitude(Double.parseDouble(etAltitude.getText().toString().trim()));
        }
        baseWorkingModeParameter.mDifType=baseWorkingModeParameter.mDifType.titleToEnum(tvDifType.getText().toString().trim());
        baseWorkingModeParameter.mBaseAutoStartup = ivSwitchAutoStartupOn.getVisibility() == View.VISIBLE;

        if(ivSwitchPpkOn.getVisibility()== View.VISIBLE){//启用PPK
            baseWorkingModeParameter.mUsePPK=true;
            baseWorkingModeParameter.ppkParameter.mRecordPointname=etRecordPointname.getText().toString().trim();
            baseWorkingModeParameter.ppkParameter.mRecordInterval= Integer.parseInt(etRecordInterval.getText().toString().trim());
        }else{
            baseWorkingModeParameter.mUsePPK=false;
        }

        //2.初始化DataLinkParameter，设置数据链模式
        DataLinkParameter dataLinkParameter=new DataLinkParameter();
        String datalink=tvDataLink.getText().toString().trim();
        dataLinkParameter.mDataLinkType=dataLinkParameter.mDataLinkType.titleToEnum(datalink);
        switch (datalink){
            case "主机网络":
                setDataLinkNet(dataLinkParameter);
                break;
            case "内置电台":
                setDataLinkUHF(dataLinkParameter);
                break;
            case "外置电台":
                setDataLinkExt(dataLinkParameter);
                break;
        }

        //3.初始化SateSystemEnableParameter，设置是否使用6大卫星参数
        SateSystemEnableParameter sateSystemEnableParameter=new SateSystemEnableParameter();
        sateSystemEnableParameter.UseGPS = ivSwitchGpsOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseGlonass = ivSwitchGlonassOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseBeiDou = ivSwitchBeidouOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseGalileo=ivSwitchGalileoOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseQZSS=ivSwitchQzssOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseSBAS=ivSwitchSbasOn.getVisibility() == View.VISIBLE;

        //4.调用接口进行命令发送
        GeoManage.getInstance().setBaseModel(baseWorkingModeParameter,dataLinkParameter,sateSystemEnableParameter);

    }

    private void setDataLinkLBand(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.isUseLBand=true;
    }

    private void setDataLinkExt(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.mExternal.Baud= Integer.parseInt(etBaud.getText().toString().trim());
    }

    private void setDataLinkUHF(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.mUHF.mChannelNumber= Integer.parseInt(tvChannelNumber.getText().toString().trim());
        //8通道为自定义通道,给mFrequencyofChannel[7]赋予自己写的值
        if(dataLinkParameter.mUHF.mChannelNumber==8){
            dataLinkParameter.mUHF.mFrequencyofChannel[7]= Double.parseDouble(etFrequency.getText().toString().trim());
        }
        dataLinkParameter.mUHF.mUhfProtocolType= dataLinkParameter.mUHF.mUhfProtocolType.titleToEnum(tvProtocol.getText().toString().trim());

    }

    private void setDataLinkNet(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.mNetwork.mConnectionMode= dataLinkParameter.mNetwork.mConnectionMode.titleToEnum(tvConnectModel.getText().toString().trim());
        dataLinkParameter.mNetwork.mAutoConnect = ivSwitchConnectNetworkOn.getVisibility() == View.VISIBLE;
        dataLinkParameter.mNetwork.mCorsParameter.mStrIP=etCorsIp.getText().toString().trim();
        dataLinkParameter.mNetwork.mCorsParameter.mPort= Integer.parseInt(etCorsPort.getText().toString().trim());
        if(tvConnectModel.getText().toString().trim().equals("TCP客户端"))
            return;
        dataLinkParameter.mNetwork.mCorsParameter.mStrUser=etCorsUser.getText().toString().trim();
        dataLinkParameter.mNetwork.mCorsParameter.mStrPassword=etCorsPass.getText().toString().trim();
        dataLinkParameter.mNetwork.mCorsParameter.mStrMountPoint=etCorsMountPoint.getText().toString().trim();
    }

    //选择对话框里面的选项后做的操作
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showChannelNum(DialogDismissEvent event) {
        String title=event.getTitle();
        String data=event.getData();
        switch (title) {
            case "启动模式":
                showStartModel(data);
                break;
            case "差分模式":
                tvDifType.setText(data);
                break;
            case "数据链":
                showDataLinkData(data);
                break;
            case "连接模式":
                tvConnectModel.setText(data);
                clCors.setVisibility(View.VISIBLE);
                if (data.equals("TCP客户端")) {//此时不需要显示CORS用户名密码，接入点
                    clCors.setVisibility(View.GONE);
                }
                break;
            case "通道":
                tvChannelNumber.setText(data);
                //只有8通道允许自定义，其他都是默认不允许修改
                int channelNum = Integer.parseInt(data);
                if (channelNum == 8) {
                    etFrequency.setEnabled(true);
                } else {
                    etFrequency.setEnabled(false);
                }
                //每一个通道对应一个频率，通道1对应mFrequencyofChannel[0]
                etFrequency.setText(String.valueOf(dataLinkParameter.mUHF.mFrequencyofChannel[channelNum - 1]));
                break;
            case "协议":
                tvProtocol.setText(data);
                break;
        }
    }

    private void showStartModel(String data) {
        tvStartModel.setText(data);
        clStartupTypeRepeat.setVisibility(View.GONE);
        if(data.equals("指定基站坐标")){
            clStartupTypeRepeat.setVisibility(VISIBLE);
        }else{
            clStartupTypeRepeat.setVisibility(View.GONE);
        }
    }

    private void onClickQzssOn() {
        ivSwitchQzssOn.setVisibility(View.GONE);
        ivSwitchQzssOff.setVisibility(View.VISIBLE);
    }

    private void onClickQzssOff() {
        ivSwitchQzssOff.setVisibility(View.GONE);
        ivSwitchQzssOn.setVisibility(View.VISIBLE);
    }

    private void onClickSbasOn() {
        ivSwitchSbasOn.setVisibility(View.GONE);
        ivSwitchSbasOff.setVisibility(View.VISIBLE);
    }

    private void onClickSbasOff() {
        ivSwitchSbasOff.setVisibility(View.GONE);
        ivSwitchSbasOn.setVisibility(View.VISIBLE);
    }

    private void onClickGalileoOn() {
        ivSwitchGalileoOn.setVisibility(View.GONE);
        ivSwitchGalileoOff.setVisibility(View.VISIBLE);
    }

    private void onClickGalileoOff() {
        ivSwitchGalileoOff.setVisibility(View.GONE);
        ivSwitchGalileoOn.setVisibility(View.VISIBLE);
    }

    private void onClickBeidouOn() {
        ivSwitchBeidouOn.setVisibility(View.GONE);
        ivSwitchBeidouOff.setVisibility(View.VISIBLE);
    }

    private void onClickBeidouOff() {
        ivSwitchBeidouOff.setVisibility(View.GONE);
        ivSwitchBeidouOn.setVisibility(View.VISIBLE);
    }

    private void onClickGlonassOn() {
        ivSwitchGlonassOn.setVisibility(View.GONE);
        ivSwitchGlonassOff.setVisibility(View.VISIBLE);
    }

    private void onClickGlonassOff() {
        ivSwitchGlonassOff.setVisibility(View.GONE);
        ivSwitchGlonassOn.setVisibility(View.VISIBLE);
    }

    private void onClickGpsOn() {
        ivSwitchGpsOn.setVisibility(View.GONE);
        ivSwitchGpsOff.setVisibility(View.VISIBLE);
    }

    private void onClickGpsOff() {
        ivSwitchGpsOff.setVisibility(View.GONE);
        ivSwitchGpsOn.setVisibility(View.VISIBLE);
    }

    private void onClickAutoConnectNetOn() {
        ivSwitchConnectNetworkOn.setVisibility(View.GONE);
        ivSwitchConnectNetworkOff.setVisibility(View.VISIBLE);
    }

    private void onClickAutoConnectNetOff() {
        ivSwitchConnectNetworkOff.setVisibility(View.GONE);
        ivSwitchConnectNetworkOn.setVisibility(View.VISIBLE);
    }

    private void onClickChannelNumber() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"通道");
        startActivity(intent);
    }

    private void onClickUhfProtocol() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"协议");
        startActivity(intent);
    }


    private void onClickConnectMode() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"连接模式");
        startActivity(intent);
    }

    private void onClickRlDataLink() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"数据链");
        intent.putExtra(ISBASE,true);
        startActivity(intent);
    }

    private void onClickPPkOn() {
        ivSwitchPpkOn.setVisibility(View.GONE);
        ivSwitchPpkOff.setVisibility(View.VISIBLE);
        clPpk.setVisibility(View.GONE);
    }

    private void onClickPPkOff() {
        ivSwitchPpkOff.setVisibility(View.GONE);
        ivSwitchPpkOn.setVisibility(View.VISIBLE);
        clPpk.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            finish();
        }else if(v==rlStartModel){
            onClickStartModel();
        }else if(v==rlDifType){
            onClickDifType();
        }else if(v==ivSwitchAutoStartupOn){
            onClickAutoStartupOn();
        }else if(v==ivSwitchAutoStartupOff){
            onClickAutoStartupOff();
        }else if(v==ivSwitchPpkOff){
            onClickPPkOff();
        }else if(v==ivSwitchPpkOn){
            onClickPPkOn();
        }else if(v==rlDataLink){
            onClickRlDataLink();
        }else if(v==rlConnectMode){
            onClickConnectMode();
        }else if(v==rlChannelNumber){
            onClickChannelNumber();
        }else if(v==rlProtocol){
            onClickUhfProtocol();
        }else if(v==btnDetermine){
            onClickBtnDetermine();
        }else if(v==ivSwitchConnectNetworkOff){
            onClickAutoConnectNetOff();
        }else if(v==ivSwitchConnectNetworkOn){
            onClickAutoConnectNetOn();
        }else if(v==ivSwitchGpsOff){
            onClickGpsOff();
        }else if(v==ivSwitchGpsOn){
            onClickGpsOn();
        }else if(v==ivSwitchGlonassOff){
            onClickGlonassOff();
        }else if(v==ivSwitchGlonassOn){
            onClickGlonassOn();
        }else if(v==ivSwitchBeidouOff){
            onClickBeidouOff();
        }else if(v==ivSwitchBeidouOn){
            onClickBeidouOn();
        }else if(v==ivSwitchGalileoOff){
            onClickGalileoOff();
        }else if(v==ivSwitchGalileoOn){
            onClickGalileoOn();
        }else if(v==ivSwitchSbasOff){
            onClickSbasOff();
        }else if(v==ivSwitchSbasOn){
            onClickSbasOn();
        }else if(v==ivSwitchQzssOff){
            onClickQzssOff();
        }else if(v==ivSwitchQzssOn){
            onClickQzssOn();
        }
    }

    private void onClickAutoStartupOff() {
        ivSwitchAutoStartupOff.setVisibility(View.GONE);
        ivSwitchAutoStartupOn.setVisibility(View.VISIBLE);
    }

    private void onClickAutoStartupOn() {
        ivSwitchAutoStartupOn.setVisibility(View.GONE);
        ivSwitchAutoStartupOff.setVisibility(View.VISIBLE);
    }

    private void onClickDifType() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"差分模式");
        startActivity(intent);
    }

    private void onClickStartModel() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"启动模式");
        startActivity(intent);
    }

    //开始动画
    private void startAnim() {
        anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        anim.setInterpolator(interpolator);
        if (anim != null) {
            flLoading.setVisibility(VISIBLE);
            ivLoading.startAnimation(anim);  //开始动画
        }
    }

    //停止动画
    private void stopAnim() {
        if (anim != null) {
            anim =null;
            flLoading.setVisibility(View.GONE);
            ivLoading.clearAnimation();
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        stopAnim();
        super.onDestroy();
    }
}
